package com.yuyu.qingmangmall.product.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.R;
import com.yuyu.qingmangmall.product.entity.BrandEntity;
import com.yuyu.qingmangmall.product.entity.CategoryBrandRelationEntity;
import com.yuyu.qingmangmall.product.service.CategoryBrandRelationService;
import com.yuyu.qingmangmall.product.vo.BrandsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 品牌分类关联
 *
 * @author yuwengsheng
 * @email 3337002363@qq.com
 * @date 2021-05-31 01:02:11
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     * @RequestParam 是传递参数的.
     * @RequestParam 用于将请求参数区数据映射到功能处理方法的参数上。
     *
     * 例：
     * public String queryUserName(@RequestParam String userName)
     * 在url中输入:localhost:8080/.../?userName=zhangsan
     * 请求中包含username参数（如/requestparam1?userName=zhang），则自动传入。
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取当前品牌的所有分类列表
     * @param brandId
     * @return
     */
//    @RequestMapping(value = "/catelog/list" ,method = RequestMethod.GET)
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam Long brandId) {

        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));

        return R.ok().put("data",data);
    }

    /**
     *  获取分类关联的品牌
     *  /product/categorybrandrelation/brands/list
     *
     *  1. Controller：处理请求，接收和校验数据
     *  2. Service接收controller传来的数据，进行业务处理
     *  3. Controller接收Service处理完的数据，封装页面指定的vo
     */
    @GetMapping("/brands/list")
    public R brandsList(@RequestParam(value = "catId") Long catId) {
        List<BrandEntity> relationList = categoryBrandRelationService.getRelation(catId);
        List<BrandsVo> collect = relationList.stream().map(item -> {
            BrandsVo brandsVo = new BrandsVo();
            brandsVo.setBrandId(item.getBrandId());
            brandsVo.setBrandName(item.getName());
            return brandsVo;
        }).collect(Collectors.toList());

        return R.ok().put("data",collect);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     *
     * @RequestBody 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；GET方式无请求体
     * 根据json字符串中的key来匹配对应实体类的属性
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        // categoryBrandRelation 类别品牌关系
//        categoryBrandRelationService.save(categoryBrandRelation);
        // saveDetail 新增品牌与分类关联关系
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
